package com.lxy.skill.controller;

import com.lxy.skill.server.SecKillServer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author lxy
 * @Date 2021/12/12 17:26
 * @Description
 */
@RestController
@RequestMapping("/test")
public class TestRabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("DirectExchange", "DirectRouting", map);
        return "ok";
    }

    @Autowired
    private SecKillServer secKillServer;

    @PostMapping("/seckill")
    public Boolean skill(@RequestParam(value="sid", required = false) String sid, @RequestParam(value = "uid", required = false) String uid){
        System.out.println("访问的秒杀服务是8033，请求的时间："+new Date()+" sid="+sid+" uid="+uid);
        boolean b = false;
        try {
            b = secKillServer.skillZookeeper(sid, uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

//    @Autowired
//    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
//
//    @GetMapping("/sendDirectMessage")
//    public String sendDirectMessage() {
//        String userId = String.valueOf(UUID.randomUUID());
//        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
//        rabbitTemplate.convertAndSend("SecKillDirectExchange", "SecKillDirectRouting", userId);
//        return "ok";
//    }

}
